package com.jk.controller.account;

import com.jk.dto.JsonResult;
import com.jk.entity.account.AccRole;
import com.jk.service.account.AccRoleService;
import com.jk.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 *  Created by yanghuan on 15-8-31.
 */
@Controller
@RequestMapping("/account/role")
public class RoleController {
    @Resource
    private AccRoleService accRoleService;

    //跳转到角色管理页面
    @RequestMapping("/page")
    public String manage() {
        return "account/role";
    }

    //获取显示的角色
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public String getList() {
        try {
            return JsonUtil.toString(accRoleService.getRoleList());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<AccRole>());
        }
    }

    //创建
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String createRole(AccRole accRole) {
        try {
            accRoleService.insert(accRole);
            return JsonUtil.toString(new JsonResult(200, "保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "保存失败"));
        }
    }

    //删除
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public String delRole(String accRoleId) {
        try {
            accRoleService.delete(accRoleId);
            return JsonUtil.toString(new JsonResult(200, "删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "删除失败"));
        }
    }

    @RequestMapping(value = "permissions")
    @ResponseBody
    public String permissions(String accRoleId) {
        try {
            return JsonUtil.toString(accRoleService.getPermissions(accRoleId));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<Map<String, Object>>());
        }
    }

    //授权
    @RequestMapping(value = "/grant", method = RequestMethod.POST)
    @ResponseBody
    public String grantPermissions(@RequestParam(value = "perIdArray[]", required = false) String[] perIdArray,
                                   @RequestParam(value = "accRoleId", required = false) String accRoleId) {
        try {
            if (null == accRoleId) {
                return JsonUtil.toString(new JsonResult(201, "请选择所授权的角色"));
            }
            if (null == perIdArray) {
                perIdArray = new String[0];
            }
            accRoleService.grantPermissions(accRoleId, perIdArray);
            return JsonUtil.toString(new JsonResult(200, "授权成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "授权失败"));
        }
    }

}
